﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace DynaSpace.Entity
{
	/// <summary>
	/// Třída reprezentující hráče na mapě
	/// </summary>
	public class Player : UpdatableEntity, IRendeable
	{
		private double animPhase;
		private PlayerAnimType animType = PlayerAnimType.RightStand;
		private Vector2 lastPos;
		private PlayerIndex index;

		/// <summary>
		/// Konstruktor <see cref="Player">entity hráče</see>.
		/// </summary>
		/// <param name="parent">Rodičovský <see cref="Playground"/></param>
		/// <param name="position">Počáteční <see cref="Vector2">pozice</see></param>
		/// <param name="index">Index hráče</param>
		public Player(Playground parent, Vector2 position, PlayerIndex index)
			: base(parent, position)
		{
			this.index = index;
			this.Size = new Vector2(0.25f, 1f); // at je hrac trochu stihlejsi nez cele pole entity
		}

		/// <summary>
		/// Updatuje stav <see cref="Player"/>
		/// </summary>
		/// <param name="gameTime">Čas hry</param>
		public override void Update(GameTime gameTime)
		{
			base.Update(gameTime); // updatovat predka

			// vybrat vhodnou animacni sekvenci
			if (lastPos != Position)
			{
				if (lastPos.X < Position.X)
					animType = PlayerAnimType.RightRun;
				else if (lastPos.X > Position.X)
					animType = PlayerAnimType.LeftRun;
				else if (animType == PlayerAnimType.LeftStand)
					animType = PlayerAnimType.LeftRun;
				else if (animType == PlayerAnimType.RightStand)
					animType = PlayerAnimType.RightRun;	

				lastPos = Position;
			}
			else if (animType == PlayerAnimType.RightRun)
				animType = PlayerAnimType.RightStand;
			else if (animType == PlayerAnimType.LeftRun)
				animType = PlayerAnimType.LeftStand;
		
			// fáze animace
			animPhase += gameTime.ElapsedGameTime.TotalMilliseconds / 100;
			animPhase = animPhase % 4;
		}

		/// <summary>
		/// Vrací index hráče
		/// </summary>
		public PlayerIndex Index
		{
			get
			{
				return index;
			}
		}

		/// <summary>
		/// Vrací aktuální typ animace
		/// </summary>
		public PlayerAnimType AnimType
		{
			get
			{
				return animType;
			}
		}

		/// <summary>
		/// Zraní hráče
		/// </summary>
		/// <param name="damage">Hodnota zranění</param>
		public virtual void Damage(float damage)
		{
			OnDead();
		}

		private void OnDead()
		{
			if (Dead != null)
				Dead.Invoke(this, new EventArgs());
		}

		/// <summary>
		/// Vyvolá se, pokud hráč zemře
		/// </summary>
		public event EventHandler Dead;

		#region IRendeable Members

		/// <summary>
		/// Vykreslí entitu <see cref="Player">hráče</see>
		/// </summary>
		/// <param name="rel">Rekativní pozice pohledu</param>
		/// <param name="sb"><see cref="SpriteBatch"/></param>
		public void Render(Microsoft.Xna.Framework.Vector2 rel, SpriteBatch sb)
		{
			int animPhaseCurrent = (int)animPhase;

			if (animType == PlayerAnimType.LeftRun || animType == PlayerAnimType.RightRun)
				animPhaseCurrent += 4;

			// převrácení animace
			bool rev = animType == PlayerAnimType.LeftRun || animType == PlayerAnimType.LeftStand;

			// vytvoření zdrojových souřadnic
			Rectangle rect;
			rect = new Rectangle(animPhaseCurrent * (int)SIZEX, 0, (int)SIZEX, (int)SIZEY);

			// vykreslení
			sb.Draw(ContentComponent.player, (Position - rel) * SIZE, rect, Color.White, 0f, Vector2.Zero,1, rev ? SpriteEffects.FlipHorizontally : SpriteEffects.None, 0f);
		}

		#endregion
	}
}
